import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  // strict: true,
  state() {
    return {
      list: JSON.parse(localStorage.getItem('todo')) || [
        { id: 100, name: "吃饭", isDone: true },
        { id: 201, name: "睡觉", isDone: false },
        { id: 103, name: "打豆豆", isDone: true },
      ],
      Str: '全部'
    }
  },
  mutations: {
    del(state, index) {
      state.list.splice(index, 1)
    },
    add(state, msg) {
      state.list.push({ id: Date.now(), name: msg, isDone: false })
    },
    changeStr(state, str) {
      state.Str = str
    },
    clearCompele(state) {
      state.list = state.list.filter(item => item.isDone == false)
    },
    allsele(state, val) {
      if (val) {
        state.list.forEach(ele => {
          ele.isDone = val
        })
      } else if (val == false) {
        state.list.forEach(ele => {
          ele.isDone = val
        })
      }
    }


  },
  getters: {
    newList(state) {
      let arr = []
      if (state.Str == '全部') {
        arr = state.list
      } else if (state.Str == '未完成') {
        arr = state.list.filter(item => item.isDone == false)
      } else if (state.Str == '已完成') {
        arr = state.list.filter(item => item.isDone)
      }
      return arr
    }
  }
})
export default store